package com.fei.controller;

import com.fei.bean.MusicBean;
import com.fei.bean.SingerBean;
import com.fei.bean.UserBean;
import com.fei.service.FileService;
import com.fei.service.MusicService;
import com.fei.service.SingerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.List;

@Controller
public class MusicController {

    @Autowired
    MusicService musicService;

    @Autowired
    SingerService singerService;

    @Autowired
    FileService fileService;

//    添加歌曲时验证是否，添加过该歌曲
    @RequestMapping("/ValidSongTitle")
    @ResponseBody
    public Boolean ValidSongTitle(String song_title){
        return musicService.ValidSongTitleService(song_title);
    }

    //歌曲管理页面
    @RequestMapping("/admin/toSingerSong")
    public String toSingerSong(Integer singer_id, Model model){
        List<MusicBean> singerMusicList = musicService.selectOneSingerIdService(singer_id);
        SingerBean singerBean = singerService.selectOneIdService(singer_id);
        model.addAttribute("musicBean",singerMusicList);
        model.addAttribute("singerBean",singerBean);
        return "singer_song";
    }

    @RequestMapping("/admin/add/insetMusic")
    public String insetMusic(MultipartFile file1,MultipartFile file2,MusicBean musicBean,Model model){
        SingerBean singerBean = singerService.selectOneIdService(musicBean.getSinger_id());

        //上传
        File path = fileService.ParentPath();
        String file1Suffix = fileService.FileSuffix(file1.getOriginalFilename());
        String file2Suffix = fileService.FileSuffix(file2.getOriginalFilename());
        String filePath = path + "//data//Music//"+singerBean.getName()+"/"+musicBean.getSong_title();
        Boolean f1 = fileService.FileUpload(filePath,"1."+file1Suffix, file1);
        Boolean f2 = fileService.FileUpload(filePath,"2."+file2Suffix,file2);

        musicBean.setImage("/Music/"+singerBean.getName()+"/"+musicBean.getSong_title()+"/1."+file1Suffix);
        musicBean.setMusic_path("/Music/"+singerBean.getName()+"/"+musicBean.getSong_title()+"/2."+file2Suffix);

        musicService.insertMusicSingerService(musicBean);
        System.out.println(musicBean);

        // 用于歌手管理页面的信息
        List<MusicBean> singerMusicList = musicService.selectOneSingerIdService(musicBean.getSinger_id());
        model.addAttribute("musicBean",singerMusicList);
        model.addAttribute("singerBean",singerBean);

        return "singer_song";
    }

    @RequestMapping("/admin/add/insetMusicList")
    public String insetMusicList(MultipartFile[] files, Integer singer_id, HttpSession session){
        SingerBean singerBean = singerService.selectOneIdService(singer_id);

        UserBean user = (UserBean)session.getAttribute("user");

        MusicBean music;

        //上传
        File path = fileService.ParentPath();
        Boolean flag = true;

        for (MultipartFile file : files){
            String fileName = fileService.FileDeletePrefix(file.getOriginalFilename());
//            说明不是文件夹
            if (fileName.length() != fileService.FileSuffix(fileName).length()){
                System.out.println("不是文件夹"+fileName);
//            该歌手所有的歌曲
                List<MusicBean> musicBeans = musicService.selectOneSingerIdService(singer_id);
//                判断该歌手是否添加过该歌曲
                if (musicBeans != null){
                    String fileNameDeleteSuffix = fileService.FileName(fileName);
                    for (MusicBean musicBean : musicBeans){
//                        说明添加过该歌曲
                        if (musicBean.getSong_title().equals(fileNameDeleteSuffix)){
                            flag = false;
                            break;
                        }
                    }
                    if (flag){
                        String fileSuffix = fileService.FileSuffix(fileName);
                        if (fileSuffix.equals("mp3")){
                            String filePath = path + "//data//Music//"+singerBean.getName()+"/"+fileNameDeleteSuffix;
                            fileService.FileUpload(filePath,"2."+fileSuffix,file);
                            music = new MusicBean();
                            music.setMusic_path("/Music/"+singerBean.getName()+"/"+fileNameDeleteSuffix+"/2."+fileSuffix);
                            music.setSong_title(fileNameDeleteSuffix);
                            music.setSinger_id(singer_id);
                            musicService.insertMusicSingerService(music);
                        }
                        flag = true;
                    }
                }
            }
        }

//
//        musicBean.setImage("/Music/"+singerBean.getName()+"/"+musicBean.getSong_title()+"/1."+file1Suffix);
//        musicBean.setMusic_path("/Music/"+singerBean.getName()+"/"+musicBean.getSong_title()+"/2."+file2Suffix);
//
//        musicService.insertMusicSingerService(musicBean);
//        System.out.println(musicBean);
//
//        // 用于歌手管理页面的信息
//        List<MusicBean> singerMusicList = musicService.selectOneSingerIdService(musicBean.getSinger_id());
//        model.addAttribute("musicBean",singerMusicList);
//        model.addAttribute("singerBean",singerBean);

        return "redirect:/admin/toSingerSong?singer_id="+singer_id;
    }

    @RequestMapping("/admin/delete/musicDelete")
    public String musicDelete(int id,Model model,int singer_id){
        //删除对应的歌曲文件，和数据库信息，以及歌单，用户歌单中对应的数据
        MusicBean musicBean = musicService.selectOneIdService(id);
        SingerBean singerBean = singerService.selectOneIdService(singer_id);
        File path = fileService.ParentPath();
        String filePath = path + "//data//Music//"+singerBean.getName()+"/"+musicBean.getSong_title();
        fileService.FileDelete(new File(filePath));
        musicService.deleteMusicOneIdService(id);

        return "redirect:/admin/toSingerSong?singer_id="+singer_id;
    }



}
